package networkListener;

/**
 * @author re0hg
 * @version 1.0
 * @date 2025/3/28
 */

import org.pcap4j.packet.*;
import org.pcap4j.packet.namednumber.TcpPort;

import java.net.InetAddress;
public class PacketProcessor {

    private final Packet packet;

    public PacketProcessor(Packet packet) {
        this.packet = packet;
    }

    public String[] processPacket() {
        EthernetPacket ethernetPacket = packet.get(EthernetPacket.class);
        if (ethernetPacket == null) {
            IpV4Packet ipV4Packet = packet.get(IpV4Packet.class);
            if (ipV4Packet == null) {
                System.out.println("Not an Ethernet or IPv4 packet");
                return null;  // 如果没有以太网包和 IPv4 包，跳过
            }
            InetAddress srcIp = ipV4Packet.getHeader().getSrcAddr();
            InetAddress dstIp = ipV4Packet.getHeader().getDstAddr();
            TcpPacket tcpPacket = packet.get(TcpPacket.class);
            if (tcpPacket == null) {
                //System.out.println("Not a TCP packet");
                return null;
            }

            return processTcpPacket(tcpPacket, srcIp, dstIp);
        }

        // 获取以太网信息
        String srcMac = ethernetPacket.getHeader().getSrcAddr().toString();
        String dstMac = ethernetPacket.getHeader().getDstAddr().toString();
        IpV4Packet ipV4Packet = packet.get(IpV4Packet.class);
        if (ipV4Packet == null) {
            System.out.println("Not an IPv4 packet");
            return null;
        }

        InetAddress srcIp = ipV4Packet.getHeader().getSrcAddr();
        InetAddress dstIp = ipV4Packet.getHeader().getDstAddr();

        // 获取 TCP 包
        TcpPacket tcpPacket = packet.get(TcpPacket.class);
        if (tcpPacket == null) {
            System.out.println("Not a TCP packet");
            return null;
        }

        // 处理 TCP 包
        return processTcpPacket(tcpPacket, srcIp, dstIp);
    }

    private String[] processTcpPacket(TcpPacket tcpPacket, InetAddress srcIp, InetAddress dstIp) {
        // 只处理端口 21 的 TCP 包（FTP 控制通道）
        int srcPort = tcpPacket.getHeader().getSrcPort().valueAsInt();
        int dstPort = tcpPacket.getHeader().getDstPort().valueAsInt();
        if (srcPort != 21 && dstPort != 21) {
            return null;  // 如果不是端口 21，跳过
        }

//        if (dstPort != 21) {
//            return null;  // 如果不是端口 21，跳过
//        }
        // 获取 TCP 包的负载数据
        if (tcpPacket.getPayload() == null) {
            System.out.println("No payload in TCP packet");
            return null;  // 如果没有负载，跳过
        }

        String payload = new String(tcpPacket.getPayload().getRawData());

        String username = null;
        String password = null;
        String status = "UNKNOWN";

        // 解析 FTP 命令和响应
        if (payload.startsWith("USER")) {
            username = payload.split(" ")[1].trim();
        } else if (payload.startsWith("PASS")) {
            password = payload.split(" ")[1].trim();
        } else if (payload.startsWith("230")) {  // FTP 服务响应：登录成功
            status = "LOGIN_SUCCEEDED";
        } else if (payload.startsWith("220")) {  // FTP 服务响应：服务已准备好
            status = "SERVICE_READY";
        } else if (payload.startsWith("331")) {  // 请求用户名
            status = "USERNAME_REQUIRED";
        } else if (payload.startsWith("530")) {  // 登录失败
            status = "LOGIN_FAILED";
        }

        // 确保返回的数据数组有 7 个字段（补充空白字段）
        return new String[] {
                "",  // Placeholder for extra data
                "" ,  // Placeholder for extra data
                srcIp.getHostAddress(),
                dstIp.getHostAddress(),
                username != null ? username : "",
                password != null ? password : "",
                status
        };
    }
}
